﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using frontend_wcf.WCFService;

namespace frontend_wcf
{
    public partial class FrmMain : Form
    {
        ServiceClient service;
        string currentTable;
        public FrmMain()
        {
            service = new ServiceClient("WSHttpBinding_IService");
            InitializeComponent();
        }

        private void btn_GetTable_Click(object sender, EventArgs e)
        {
            if (cbx_TableName.SelectedItem == null || cbx_TableName.SelectedItem.ToString() == "")
                return;
            currentTable = cbx_TableName.SelectedItem.ToString();
            DataSet dataset = service.getData(currentTable, null);
            dataGrid.DataSource = dataset.Tables[currentTable].DefaultView;

            grp_TableTools.Text = "Tools for " + currentTable;
            grp_TableTools.Visible = true;
        }

        private void btn_CreateTable_Click(object sender, EventArgs e)
        {
            if (txt_TableName.Text != "")
            {
                service.createTable(txt_TableName.Text);
                btn_GetTable_Click(sender, e);
            }
        }

        private void btn_AddColumn_Click(object sender, EventArgs e)
        {
            if (txt_ColumnName.Text == "")
                return;

            service.createColumn(currentTable, txt_ColumnName.Text, cbx_ColumnType.SelectedItem.ToString());
            btn_GetTable_Click(sender, e);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            cbx_ColumnType.SelectedIndex = 0;
        }

        private void btn_AddData_Click(object sender, EventArgs e)
        {
            FrmAddData frmAddData = new FrmAddData(service, currentTable);
            DialogResult dresult = frmAddData.ShowDialog();
            if (dresult == DialogResult.Yes)
            {
                btn_GetTable_Click(sender, e);
            }
        }

        private void cbx_TableName_DropDown(object sender, EventArgs e)
        {
            cbx_TableName.Items.Clear();
            string[] tables = service.getTables();
            cbx_TableName.Items.AddRange(tables);
            if (cbx_TableName.Items.Count > 0)
                cbx_TableName.SelectedIndex = 0;
        }

        private void btn_DeleteData_Click(object sender, EventArgs e)
        {
            int id = 0;
            try
            {
                if (dataGrid.SelectedRows.Count > 0)
                {
                    id = Convert.ToInt32(dataGrid.SelectedRows[0].Cells[0].Value);
                }
                else if (dataGrid.SelectedCells.Count > 0)
                {
                    id = Convert.ToInt32(dataGrid.SelectedCells[0].Value);
                }
                service.deleteData(currentTable, id);
                btn_GetTable_Click(sender, e);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void dataGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            dataGrid.DataSource = null;
            MessageBox.Show("Unable to view DataGrid due to BLOBs");
        }
    }
}
